package com.company.algo.tree;

import com.company.algo.pojo.TreeNode;

/**
 * 114. 二叉树展开为链表
 */
public class Flatten {

    void flatten(TreeNode root){
        if (root == null) return;
        flatten(root.left);
        flatten(root.right);
        // 1、左右子树已经被拉平成一条链表
        TreeNode left = root.left;
        TreeNode right = root.right;
        // 2. 将左子树作为右子树
        root.right = left;
        // 3. 将原右子树接到当前右子树的末端
        TreeNode p = root;
        while (p.right != null){
            p = p.right;
        }
        p.right = right;
    }
}
